﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text.RegularExpressions;

public partial class BaseData_ProductColumn : System.Web.UI.Page
{
    protected void Page_PreInit(object sender, EventArgs e)
    {
        Page.Theme = BLL.Common.GetWSMThemes();
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        Check.CheckPermission("200300103");
        if (!this.IsPostBack)
        {
            GetColumnList();
        }
    }

    private void GetColumnList()
    {
        TreeView1.Nodes.Clear();
        DataTable dt = new DataTable();
        dt = new BLL.SHANGPIN().GetSPCOMMODITY_TYPEList_Order().Tables[0];

        DataView dv = new DataView(dt);
        dv.RowFilter = " CT_FCODE=0";
        foreach (DataRowView drv in dv)
        {
            TreeNode tn = new TreeNode();
            tn.Text = drv["CT_NAME"].ToString() ;
            tn.Value = drv["CT_CODE"].ToString();
            tn.ToolTip = drv["CT_NOTE"].ToString() + "(包含商品数量：" + drv["spcounts"].ToString() + ")";
            tn.Target = drv["CT_FIELD1"].ToString();
            TreeView1.Nodes.Add(tn);
            ChildNodes(dt, tn);
        }
    }

    private void ChildNodes(DataTable dt, TreeNode tn)
    {
        DataView dv = new DataView(dt);
        dv.RowFilter = " CT_FCODE=" + tn.Value;
        foreach (DataRowView drv in dv)
        {
            TreeNode tn2 = new TreeNode();
            tn2.Text = drv["CT_NAME"].ToString();
            tn2.Value = drv["CT_CODE"].ToString();
            tn2.ToolTip = drv["CT_NOTE"].ToString() + "(包含商品数量：" + drv["spcounts"].ToString() + ")";
            tn2.Target = drv["CT_FIELD1"].ToString();
            tn.ChildNodes.Add(tn2);
            ChildNodes(dt, tn2);
        }
    }


    protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
    {
        TreeView1.SelectedNodeStyle.Font.Italic = true;
        this.lblEditMsg.Text = "";
        this.lblMsgDel.Text = "";
        this.lblmsgnew.Text = "";
        string str = TreeView1.SelectedNode.Text;
        this.txtEditNode.Text = this.Label1.Text = str.Substring(str.LastIndexOf('-')+1);
        this.txtEditNodeS.Text = TreeView1.SelectedNode.Target;
        this.txtEditNodeR.Text = TreeView1.SelectedNode.ToolTip;
        this.Label2.Text = TreeView1.SelectedNode.Value;
        if (TreeView1.SelectedNode.Depth.ToString() == "0")
        {
            this.Label2.ToolTip = "0";
        }
        else
        {
            this.Label2.ToolTip = TreeView1.SelectedNode.Parent.Value;
        }
    }

    #region 操作节点
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (!Check.CheckPermission("200300103", "00001"))
        {
            Share.Alert(this.Page,"无权操作"); return;
        }
        if (this.Label2.Text.Trim() == "")
        {
            lblmsgnew.Text = "请先选择左边您要操作的节点";
        }
        else
        {
            if (!CheckInputInteger(this.txtNewNodeS.Text.Trim()) || this.txtNewNodeS.Text.Trim().Length > 4)
            {
                Share.Alert(this.Page, "节点顺序最多只能输入4位非零开始的正整数"); return;
            }
            string cname = this.txtNewNode.Text.Trim();
            string intro = this.txtNewNodeR.Text.Trim();
            string order = this.txtNewNodeS.Text.Trim();
            int parentid = Int32.Parse(this.Label2.Text);
            if (cname != "")
            {
                if (new BLL.SHANGPIN().AddSPCOMMODITY_TYPE(parentid, cname, intro, order))
                {
                    //GetColumnList();
                    Share.InsertLog("200300103", "00001");
                    Response.Redirect("ProductColumn.aspx");
                }
                else
                {
                    lblmsgnew.Text = "添加失败";
                    Share.Alert(this.Page, "同一层节点不能有相同编号"); return;
                }
            }
            else
            {
                lblmsgnew.Text = "输入节点名";
            }
        }
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        if (!Check.CheckPermission("200300103", "00002"))
        {
            Share.Alert(this.Page,"无权操作"); return;
        }
        if (this.Label2.Text.Trim() == "")
        {
            lblEditMsg.Text = "请先选择左边您要操作的节点";
        }
        else
        {
            if (!CheckInputInteger(this.txtEditNodeS.Text.Trim()) || this.txtEditNodeS.Text.Trim().Length>4)
            {
                Share.Alert(this.Page, "节点顺序最多只能输入4位非零开始的正整数"); return;
            }
            int cid = Int32.Parse(Label2.Text);
            int parentid = Int32.Parse(this.Label2.ToolTip);
            string cname = this.txtEditNode.Text.Trim();
            string intro = this.txtEditNodeR.Text.Trim();
            string order = this.txtEditNodeS.Text.Trim();
            if (new BLL.SHANGPIN().UpDateSPCOMMODITY_TYPE(cid, parentid, cname, intro, order))
            {
                Share.InsertLog("200300103", "00002");
                Response.Redirect("ProductColumn.aspx");
            }
            else
            {
                lblEditMsg.Text = "编辑失败";
                Share.Alert(this.Page, "同一层节点不能有相同编号"); return;
            }
        }
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        if (!Check.CheckPermission("200300103", "00003"))
        {
            Share.Alert(this.Page,"无权操作"); return;
        }
        if (this.Label2.Text.Trim() == "")
        {
            lblMsgDel.Text = "请先选择左边您要操作的节点";
        }
        else
        {
            if (new BLL.COMMODITY_CT().SP_COMMODITY_CT_GetByCOMDTYPECODE(Label2.Text) > 0)
            {
                Share.Alert(this.Page, "该分类下有商品,不能删除!"); return;
            }

            int cid = Int32.Parse(Label2.Text);
            if (new BLL.SHANGPIN().DeleteSPCOMMODITY_TYPE(cid))
            {
                Share.InsertLog("200300103", "00003");
                Response.Redirect("ProductColumn.aspx");
            }
            else
            {
                lblMsgDel.Text = "该节点内有子节点,不可删";
            }
        }
    }
    #endregion
    protected void Button4_Click(object sender, EventArgs e)
    {
        if (!Check.CheckPermission("200300103", "00001"))
        {
            Share.Alert(this.Page,"无权操作"); return;
        }
        if (!CheckInputInteger(this.txtNewNodeS.Text.Trim()) || this.txtNewNodeS.Text.Trim().Length > 4)
        {
            Share.Alert(this.Page, "节点顺序最多只能输入4位非零开始的正整数"); return;
        }
        string cname = this.txtNewNode.Text.Trim();
        string intro = this.txtNewNodeR.Text.Trim();
        string order = this.txtNewNodeS.Text.Trim();
        if (cname != "")
        {
            if (new BLL.SHANGPIN().AddSPCOMMODITY_TYPE(0, cname, intro, order))
            {
                Share.InsertLog("200300103", "00001");
                Response.Redirect("ProductColumn.aspx");
            }
            else
            {
                lblmsgnew.Text = "添加失败";
                Share.Alert(this.Page, "同一层节点不能有相同编号"); return;
            }
        }
        else
        {
            lblmsgnew.Text = "输入节点名";
        }
    }
    protected void Buttonzdy_Click(object sender, EventArgs e)
    {
        if (!Check.CheckPermission("200300103", "00002"))
        {
            Share.Alert(this.Page, "无权操作"); return;
        }
        if (this.Label2.Text.Trim() == "")
        {
            lblEditMsg.Text = "请先选择左边您要操作的节点";
        }
        else
        {
            int cid = Int32.Parse(Label2.Text);
            Response.Redirect("ProductColumnSP.aspx?code=" + cid.ToString());
        }
    }
    /// <summary>
    /// 验证输入为正整数
    /// </summary>
    /// <param name="str"></param>
    /// <returns></returns>
    private bool CheckInputInteger(string str)
    {
        Regex reg = new Regex("^[1-9][0-9]*$");
        if (!reg.IsMatch(str))
        {
            return false; 
        }
        return true;
    }
}
